package com.sugon.controller.module;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.entity.ModuleSpDemandItemEntity;
import com.sugon.modules.utils.DateUtil;
import com.sugon.service.ModuleSpDemandItemService;
import com.sugon.service.ModuleSpDemandPlanService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 模具型芯需求明细Controller
 *
 * @author sugon
 * @date 2022-07-11 17:08:50
 */
@Controller
@RequestMapping("modulespdemanditem")
public class ModuleSpDemandItemController {

    @Autowired
    private ModuleSpDemandItemService moduleSpDemandItemService;
    @Resource
    private ModuleSpDemandPlanService moldSpDemandPlanService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        /*
            两种查看情形：
            1、查看指定计划的采购明细
            2、型芯采购计划调整期的查看
         */
        String planId = MapUtil.getStr(params, "planId");
        if (StrUtil.isEmpty(planId)) {
            params.put("demandMonth", DateUtil.nextMonth());
        }
        //查询列表数据
        Query query = new Query(params);
        List<ModuleSpDemandItemEntity> moduleSpDemandItemList = moduleSpDemandItemService.queryList(query);
        int total = moduleSpDemandItemService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(moduleSpDemandItemList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    @AutoLog("模具型芯需求明细--添加")
    @RequestMapping("/addDemandItem")
    @ResponseBody
    public R addDemandItem(@RequestBody String[] ids) {
        return moduleSpDemandItemService.batchAddDemandItem(ids) ? R.ok() : R.error("添加失败，请稍后重试！");
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ModuleSpDemandItemEntity moduleSpDemandItem = moduleSpDemandItemService.queryObject(id);

        return R.ok().put("moduleSpDemandItem", moduleSpDemandItem);
    }

    /**
     * 保存
     */
    @AutoLog("模具型芯需求明细--保存")
    @RequestMapping("/save")
    @ResponseBody
    public R save(@RequestBody ModuleSpDemandItemEntity moduleSpDemandItem) {
        moduleSpDemandItemService.save(moduleSpDemandItem);

        return R.ok();
    }

    /**
     * 修改
     */
    @AutoLog("模具型芯需求明细--修改")
    @RequestMapping("/update")
    @ResponseBody
    public R update(@RequestBody ModuleSpDemandItemEntity moduleSpDemandItem) {
        moduleSpDemandItemService.update(moduleSpDemandItem);
        return R.ok();
    }

    @AutoLog("模具型芯需求明细--调整")
    @RequestMapping("/adjust")
    @ResponseBody
    public R adjust(ModuleSpDemandItemEntity moduleSpDemandItem) {
        moduleSpDemandItemService.update(moduleSpDemandItem);
        return R.ok();
    }

    @RequestMapping("/generate")
    @ResponseBody
    public R generate() {
        // 校验所有的车间型芯需求明细都设置需求数和预计到货日后，在执行生成操作
        // 校验是否已添加车间型芯需求明细
        Map<String, Object> param = MapUtil.newHashMap();
        param.put("nextMonth", com.sugon.modules.utils.DateUtil.nextMonth());
        List<ModuleSpDemandItemEntity> demandItemList = moduleSpDemandItemService.queryList(param);
        if (CollectionUtil.isEmpty(demandItemList)) {
            return R.error("请先添加车间型芯需求明细后再生成制作明细！");
        }
        // 校验是否所有车间型芯需求明细是否已设置需求数和预计到货日
        demandItemList = demandItemList.stream().filter(item -> item.getDemandNum() == null || item.getEta() == null).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(demandItemList)) {
            return R.error("请确保已设置所有需求数和预计到货时间！");
        }
        moldSpDemandPlanService.generateMoldSpPlan();
        return R.ok();
    }

    /**
     * 删除
     */
    @AutoLog("模具型芯需求明细--删除")
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        moduleSpDemandItemService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ModuleSpDemandItemEntity> list = moduleSpDemandItemService.queryList(params);

        return R.ok().put("list", list);
    }
}
